package com.lagou.zk;


import com.alibaba.fastjson.JSON;
import com.lagou.zk_demo.ZkStrSerializer;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class JDBCUtils {

    private static HikariDataSource ds;

    private static final ZkClient zkClient;

    private static final String path = "/jdbc";

    static  {
        zkClient = new ZkClient("localhost:12181,localhost:22181");
        zkClient.setZkSerializer(new ZkStrSerializer());

        updateDs();

        //监听data变化
        zkClient.subscribeDataChanges(path, new IZkDataListener() {
            public void handleDataChange(String s, Object o) {
                Date d = new Date();
                DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                System.out.println(sdf.format(d) + s + " is changed");
                ds.close();
                updateDs();
            }

            public void handleDataDeleted(String s) {
                System.out.println(s + " is deleted");
                ds.close();
            }
        });

    }

    //获取ds连接池
    private static void updateDs() {
        Object conf = zkClient.readData(path);
        JDBCConfig jdbcConfig = JSON.parseObject(conf.toString(), JDBCConfig.class);

        System.out.println(jdbcConfig);

        HikariConfig config = new HikariConfig();
        config.setMaximumPoolSize(100);
        config.setDriverClassName(jdbcConfig.getDriver());
        config.setJdbcUrl(jdbcConfig.getUrl());
        config.setUsername(jdbcConfig.getUsername());
        config.setPassword(jdbcConfig.getPassword());

        ds = new HikariDataSource(config);
    }

    // 获取连接对象
    public static java.sql.Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    //释放资源
    public static void close(Statement stmt,Connection conn){
        if (stmt!=null) {
            try {
                stmt.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        if (conn!=null){
            try {
                conn.close();//归还连接
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
    public static void close(ResultSet rs, Statement stmt, Connection conn){
        if (rs!=null) {
            try {
                rs.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        if (stmt!=null) {
            try {
                stmt.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        if (conn!=null){
            try {
                conn.close();//归还连接
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
}

